package vocabularyhub.io.impl;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import vocabularyhub.io.model.IWriter;
import vocabularyhub.word.model.IWord;
import vocabularyhub.word.mw.thesaurus.model.IThesaurusItem;
import vocabularyhub.word.mw.thesaurus.model.IThesaurusWord;
import vocabularyhub.word.process.util.SerializeUtil;

public class AnkiFormatWriter implements IWriter {

    static Logger logger = Logger.getLogger(AnkiFormatWriter.class);

    String WORD_SEPARATOR = " / ";
    int wordCountLimit = 3;
    String wordColor = "#106547";
    String meaningColor = "#3E0EA0";
    String sentenceColor = null;
    boolean skipSyn = false;
    boolean skipRel = true;
    boolean skipAnt = false;
    boolean skipNear = true;

    @Override
    public boolean writeToFile(List<IWord> words, File file)
    throws FileNotFoundException, IOException {

        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        int i = 0;
        logger.info(String.format("Writing %d words to anki format file: %s",
                words.size(), file));
        for (Object word : words) {
            if (i++ == 1000) {
                i = 0;
                writer.flush();
            }
            if (word instanceof IThesaurusWord) {
                IThesaurusWord tword = (IThesaurusWord) word;

                writer.append(tword.getWord()).append("\t");
                IThesaurusItem items[] = tword.getThesaurusItems();
                boolean bColor = false;
                if ((items != null) && (items.length != 0)) {
                    for (IThesaurusItem item : items) {

                        if ((meaningColor != null)
                                && (meaningColor.equals("") == false)) {
                            bColor = true;
                        } else {
                            bColor = false;
                        }
                        if (bColor) {
                            writer.append("<span style=\"color:" + meaningColor
                                    + ";\">");
                        }
                        writer.append(item.getIndex() + ".");
                        writer.append(item.getMeaning());
                        if (bColor) {
                            writer.append("</span>");
                        }
                        writer.append("<br>");

                        if ((sentenceColor != null)
                                && (sentenceColor.equals("") == false)) {
                            bColor = true;
                        } else {
                            bColor = false;
                        }
                        if (item.getSentence() != null) {

                            for (String s : item.getSentence()) {
                                if (bColor) {
                                    writer.append("<span style=\"color:"
                                            + sentenceColor + ";\">");
                                }
                                writer.append("eg.").append(s);
                                if (bColor) {
                                    writer.append("</span>");
                                }
                                writer.append("<br>");

                            }
                        } else {
                            // writer.append(" ");
                        }

                        /* write word net */
                        if ((wordColor != null)
                                && (wordColor.equals("") == false)) {
                            bColor = true;
                        } else {
                            bColor = false;
                        }
                        if (skipSyn == false) {
                            if (item.getSyn() != null) {
                                int count = 0;
                                writer.append("Synonyms: ");
                                for (String s : item.getSyn()) {
                                    if (count++ > wordCountLimit) {
                                        break;
                                    }
                                    if (bColor) {
                                        writer.append("<span style=\"color:"
                                                + wordColor + ";\">");
                                    }
                                    writer.append(s);
                                    if (bColor) {
                                        writer.append("</span>");
                                    }
                                    writer.append(WORD_SEPARATOR);
                                }
                                writer.append("<br>");
                            }
                        }
                        if (skipRel == false) {
                            if (item.getRel() != null) {
                                int count = 0;
                                writer.append("Related: ");
                                for (String s : item.getRel()) {
                                    if (count++ > wordCountLimit) {
                                        break;
                                    }
                                    if (bColor) {
                                        writer.append("<span style=\"color:"
                                                + wordColor + ";\">");
                                    }
                                    writer.append(s);
                                    if (bColor) {
                                        writer.append("</span>");
                                    }
                                    writer.append(WORD_SEPARATOR);
                                }
                                writer.append("<br>");
                            }
                        }
                        if (skipAnt == false) {
                            if (item.getAnt() != null) {
                                int count = 0;
                                writer.append("Antonyms: ");
                                for (String s : item.getAnt()) {
                                    if (count++ > wordCountLimit) {
                                        break;
                                    }
                                    if (bColor) {
                                        writer.append("<span style=\"color:"
                                                + wordColor + ";\">");
                                    }
                                    writer.append(s);
                                    if (bColor) {
                                        writer.append("</span>");
                                    }
                                    writer.append(WORD_SEPARATOR);

                                }
                                writer.append("<br>");
                            }
                        }
                        if (skipNear == false) {
                            if (item.getNear() != null) {
                                int count = 0;
                                writer.append("Near Antonyms: ");
                                for (String s : item.getNear()) {
                                    if (count++ > wordCountLimit) {
                                        break;
                                    }
                                    if (bColor) {
                                        writer.append("<span style=\"color:"
                                                + wordColor + ";\">");
                                    }
                                    writer.append(s);
                                    if (bColor) {
                                        writer.append("</span>");
                                    }
                                    writer.append(WORD_SEPARATOR);

                                }
                                writer.append("<br>");
                            }
                        }

                    }

                }
            }// end if instance of IThesaurusWord
            writer.append(System.getProperty("line.separator"));
        }// end for words
        writer.close();

        return true;
    }

    public static void main(String args[]) throws FileNotFoundException,
    IOException {
        new AnkiFormatWriter().quickTest();
    }

    public void quickTest() throws FileNotFoundException, IOException {
        File input = new File("contents\\redCore.bin");
        File output = new File("contents\\redCore.anki.txt");
        List<IWord> words = SerializeUtil.deserialize(input);

        this.writeToFile(words, output);

    }

}
